using CodePlex.TfsLibrary.ObjectModel;

namespace CodePlex.CodePlexClientLibrary
{
    [Command("checkout", "Check out a working copy from the repository.", MinArgs = 1, MaxArgs = 2, AltName = "co", UsageSummary = "<project>[:/path] [destination] [options]",
        UsageDescription = @"If path is omitted, the entire project is retrieved.

If destination is omitted, the working copy is placed in the current
directory.")]
    public class CheckoutCommand_CodePlex : CheckoutCommand
    {
        readonly ICodePlexProjectInfoService projectInfoService;

        public CheckoutCommand_CodePlex(ICodePlexProjectInfoService projectInfoService)
        {
            this.projectInfoService = projectInfoService;
        }

        protected override void GetCheckoutParameters(out string tfsUrl,
                                                      out string serverPath,
                                                      out string destinationFolder)
        {
            string projectName;
            string projectPath;

            CodePlexClientUtil.ParseProjectName(Arguments[0], out projectName, out projectPath);

            CodePlexTfsProjectInfo info = projectInfoService.GetTfsInfoForProject(projectName);

            tfsUrl = info.TfsUrl;
            serverPath = TfsUtil.CombineProjectPath(info.ServerPath, projectPath.TrimStart('/'));

            destinationFolder = CodePlexClientUtil.GetFullPath(".");
            if (Arguments.Count > 1)
                destinationFolder = CodePlexClientUtil.GetFullPath(Arguments[1]);
        }
    }
}